﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Slja.Model;
using Slja.BLL;
using Slja.Utility;
using FrameWork.web.Web.UC;


namespace FrameWork.web.Web.Project
{
    public partial class OrderDetail : System.Web.UI.Page
    {
        protected int OrderID
        {
            get {
                return int.Parse(Request.QueryString["ID"]);
            }
        }
        OrderBLL _bll;
        protected OrderBLL orderBLL        
        {
            get { if (_bll == null) { _bll = new OrderBLL(); }
            return _bll;
            }
        }
        protected OrderExtModel OrderModel
        {
            get
            {
                if (ViewState["_OrderMode"] == null)
                {
                    ViewState["_OrderMode"] = orderBLL.GetModelExt(OrderID,true);
                }
                return (OrderExtModel)ViewState["_OrderMode"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            BasicInfo1.Order = OrderModel;
            WorkTeam1.Order = OrderModel;
            DesignWork1.Order = OrderModel;
            PriceWork1.Order = OrderModel;
            OrderLog1.OrderID = OrderID;
            ButtonTool1.Order = OrderModel;

            ButtonTool1.CommandHandle += new CommandEventHandler(ButtonTool1_CommandHandle);
            if (!IsPostBack)
            {
            }
           // string url= Request.UrlReferrer.Authority;
        }
        protected override void OnPreRender(EventArgs e)
        {
            OrderSectionBase sec = null;
            for (int i = 0; i < DIVContainer.Controls.Count; i++)
            {
                sec = DIVContainer.Controls[i] as OrderSectionBase;
                if (sec != null && sec.OpRight == Components.OpRightState.Hidden)
                {
                    sec.Visible = false;
                }
            }
            base.OnPreRender(e);
        }

        void ButtonTool1_CommandHandle(object sender, CommandEventArgs e)
        {
            Btn btnType = Btn.None;

            btnType = (Btn)Enum.Parse(typeof(Btn), e.CommandName);
           // if (Enum.TryParse<Btn>(e.CommandName, out btnType))
            {

                switch (btnType)
                {
                    case Btn.Submit: //提交
                    case Btn.Save:  //保存                        
                        SaveOrSubmit(btnType);
                        WebCommon.GoMsgPage(btnType.ToDescription()+"成功！",MsgState.successful, Request.UrlReferrer.PathAndQuery);
                        break;

                }
            }
            //else
            //{
            //    WebCommon.AlertMsg(Page, "功能开发中");
            //}
        }
        //执行保存或提交
        private void SaveOrSubmit(Btn btnSate)
        {
            //bool IsOPData = false;
            if (btnSate == Btn.Submit
                || btnSate == Btn.Save)
            {
                OrderSectionBase sec = null;
                for (int i = 0; i < DIVContainer.Controls.Count; i++)
                {
                    sec = DIVContainer.Controls[i] as OrderSectionBase;
                    if (sec != null && sec.OpRight == Components.OpRightState.Editable)
                    {
                        sec.SaveData();//保存
                       // IsOPData = true;
                    }
                }
            }
            if (btnSate == Btn.Submit)
            {
                OrderSubmitModel submit = new OrderSubmitModel();
                {
                    submit.BackState = null;                   
                    submit.OrderID = OrderModel.ID;
                    submit.SubmitSaleStateLog = OrderModel.SaleStateLog | (int)OrderModel.Enum_SaleState* 2 ;
                    string NextUserName;
                    int NextUser = WebCommon.GenerateNextStepPrincipal(DIVContainer, OrderModel, Btn.Submit, (int)OrderModel.Enum_SaleState * 2, out NextUserName);
                    submit.NextOpUserID = NextUser;
                    submit.NextOpUserName = NextUserName;
                    
                }
                OrderLogModel log = new OrderLogModel();
                {
                    log.CurState = (int)OrderModel.Enum_SaleState;
                    log.Dtm = DateTime.Now;
                    log.NextState = (int)OrderModel.Enum_SaleState * 2;
                    log.NextUser = submit.NextOpUserID;
                    log.Note = "";
                    log.OpType = (int)Btn.Submit;
                    log.OrderID = OrderModel.ID;
                    log.UserID = Common.Get_UserID;
                    log.UserName = UserData.GetUserData.U_CName;                    
                }
                orderBLL.SubmitWithLog(submit, log);
            }
        }
      
    }
}